from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from datetime import datetime
from selenium.webdriver.common.action_chains import ActionChains


class Base():
    #初始化浏览器
    def __init__(self, driver):
        self.driver = driver

    #获取地址
    def open_url(self, url):
        self.driver.get(url)

    def find_element(self, loc):
        try:
            #正确设置显式等待条件
            WebDriverWait(self.driver, 20, 0.5).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            data_time = datetime.now().strftime('%Y:%m:%d: - %H:%M:%S')
            self.driver.save_screenshot(f'error_{data_time}.png')
            print('未定位到元素', e)

    #点击
    def click(self, loc):
        self.find_element(loc).click()

    #输入
    def send_keys(self, loc, info):
        self.find_element(loc).send_keys(info)

    #悬停
    def ActionChains(self, loc):
        ActionChains(self.driver).move_to_element(self.find_element(loc)).perform()

    #断言方法
    def open_text(self, loc):
        return self.find_element(loc).text
